<template>
    <el-dialog
        v-model="configCenterRefreshDialogVisible"
        title="刷新配置"
        width="30%"
        align-center
        :show-close="false"
        :close-on-click-modal="false"
    >
        <div class="formContainer">
            <el-form :model="configCenterForm" :rules="configCenterRules" ref="configCenterFormRef">
                <el-form-item label="所属微服务" label-width="20%" prop="application">
                    <el-select v-model="configCenterForm.application" clearable placeholder="所属微服务" style="width: 100%">
                        <el-option :value="item.applicationName" :label="item.applicationName" v-for="item in applicationList" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="cancelDialog()">取 消</el-button>
                <el-button type="primary" :loading="saveLoading" @click="submitDialog()">
                    刷新配置
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script>
import {applicationInfoQuery, applicationNacosInfoQuery} from "@/request/service";
import {refreshConfigInfo} from "@/request/configCenter";
import {ElNotification} from "element-plus";

export default {
    name: "ServiceRefreshConfigCenterDialog",
    data() {
        return {
            configCenterForm: {
                application: ''
            },
            configCenterRules: {
                application: [{ required: true, message: '所属微服务不能为空', trigger: 'blur' }]
            },
            applicationList: [],
            saveLoading: false
        }
    },
    methods: {
        cancelDialog() {
            this.$store.commit('changeConfigCenterRefreshDialogVisible', false);
        },
        submitDialog() {
            this.saveLoading = true
            // 查询当前选中的微服务的实例个数
            applicationNacosInfoQuery({applicationName: this.configCenterForm.application}).then(res => {
                if (res.code === '200') {
                    let serviceInstance = res.data
                    let paramDataInfo = {
                        application: this.configCenterForm.application
                    }
                    for (let i = 0; i < serviceInstance.length; i++) {
                        refreshConfigInfo(paramDataInfo).then(res => {
                            if (res.code !== '200') {
                                ElNotification({
                                    title: 'Error',
                                    message: res.message,
                                    type: 'error'
                                })
                                return;
                            }
                        })
                    }
                    ElNotification({
                        title: 'Success',
                        message: res.message,
                        type: 'success'
                    })
                    this.$store.commit('changeConfigCenterRefreshDialogVisible', false);
                }
                this.saveLoading = false
            }).catch(res => {
                this.saveLoading = false
            })
        }
    },
    computed: {
        configCenterRefreshDialogVisible() {
            return this.$store.state.configCenterRefreshDialogVisible;
        }
    },
    created() {
        applicationInfoQuery({}).then(res => {
            this.applicationList = res.data
        })
    }
}
</script>

<style scoped>

</style>